<template>
  <!-- 商品列表项 -->
  <view class="goods-item">
    <!-- 左侧图片 -->
    <view class="goods-item_left">
      <!-- 使用 v-if 指令控制 radio 组件的显示与隐藏 -->
      <radio v-if="showRadio" :checked="goodsItem.goods_state" color="#C00000" @click="radioHandle"></radio>
      <image class="goods-pic" :src="goodsItem.goods_small_logo || defaultPicture"></image>
    </view>

    <!-- 右侧商品信息 -->
    <view class="goods-info">
      <view class="goods-name">{{goodsItem.goods_name}}</view>
      <view class="goods-price_num">
        <view class="goods-price">￥{{goodsItem.goods_price | priceHandle}}</view>
        <uni-number-box v-if="showNum" :min="0" :value="goodsItem.goods_count" @change="numChange"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 默认显示图片
        defaultPicture: 'https://img2.baidu.com/it/u=2166732786,2149482182&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375'
      };
    },

    props: {
      goodsItem: { // 商品项对象
        type: Object,
        default: {}
      },
      showRadio: { //是否显示可选按钮
        type: Boolean,
        default: false
      },
      showNum: { // 是否展示价格右侧的 NumberBox 组件
        type: Boolean,
        default: false
      }
    },

    onLoad() {

    },

    // 处理商品价格保留两位小数
    filters: {
      priceHandle(num) {
        return Number(num).toFixed(2)
      }
    },

    methods: {
      // 点击按钮是否选中该商品
      radioHandle() {
        // 把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
        this.$emit('radioHandle', {
          goods_id: this.goodsItem.goods_id,
          // 商品最新的勾选状态
          goods_state: !this.goodsItem.goods_state,
        })
      },

      // 商品数量改变
      numChange(val) {
        // console.log(val)
        this.$emit('numChange', {
          goods_id: this.goodsItem.goods_id,
          goods_count: +val
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    // 让 goods-item 项占满整个屏幕的宽度
    width: 750rpx;
    // 设置盒模型为 border-box
    box-sizing: border-box;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;

    &_left {
      display: flex;
      margin-right: 5px;
      justify-content: space-between;
      align-items: center;

      .goods-pic {
        display: block;
        width: 100px;
        height: 100px;
      }
    }

    .goods-info {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
      }

      .goods-price_num {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .goods-price {
          font-size: 16px;
          color: #c00000;
        }
      }


    }
  }
</style>
